using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Services;
using MainGame.GameSound;

namespace MainGame.GamePlay
{
  /// <summary>
  /// Gestiamo la logica del giocatore, ascoltando l'evento di fine turno e
  /// i clicks su una carta
  /// </summary>
  public abstract class PlayerLayer : AILayer
  {

    public PlayerLayer(Game game)
      : base(game)
    {
      UpdateOrder = (int)ComponentOrders.LOGIC;
    }

    public override void Initialize()
    {
      Game.GetBoardRenderer().ClickPlayerCard += new ClickPlayerCard(BoardManager_ClickPlayerCard);
      Game.GetBoardRenderer().ReleasePlayerCard += new ReleasePlayerCard(BoardManager_ReleasePlayerCard);
      Game.GetBoardRenderer().ClickPack += new ClickPack(BoardManager_ClickPack); 
      Game.GetBoardRenderer().ClickExtracted += new ClickExtracted(BoardManager_ClickExtracted);
      //Game.GetUIRenderer().OnTurnOver += new OnTurnOver(BoardManager_OnTurnOver);

      base.Initialize();
    }

    protected virtual void BoardManager_ClickPlayerCard(int player_index, int card_index)
    {
      // Play a sound
      var sound_manager = Game.Services.GetService(typeof(ISoundManager<GamePlaySounds>)) as ISoundManager<GamePlaySounds>;
      sound_manager.PlaySound((GamePlaySounds)random.Next((int)GamePlaySounds.CardFlip1, (int)GamePlaySounds.CardFlip5 + 1));
    }

    protected virtual void BoardManager_ClickExtracted(int player_index)
    {

    }

    protected virtual void BoardManager_ReleasePlayerCard(int player_index, int card_index)
    {

    }

    /// <summary>
    /// Estrae nuove carte dal mazzo e le mette sul tavolo
    /// </summary>
    /// <param name="player_index">L'indice del giocatore</param>
    /// <param name="card_quantity">Il numero di carte da estrarre</param>
    void BoardManager_ClickPack(int player_index, int card_quantity)
    {
      if (pack.Cards.Count() > 0)
      {
        for (int i = 0; i < card_quantity; ++i)
        {
          // Move card from pack to discarded cards slot
          extractedCards.Cards.Add(pack.Cards.First());
          pack.Cards = pack.Cards.Skip(1).ToList();
          // Play sound
#warning THIS SHOULD BE CALLED BY BOARD RENDERER (WHAT HAPPENS IF TWO OR MORE CARDS ARE EXTRACTED?)
          //var soundManager = Game.Services.GetService(typeof(ISoundManager<GamePlaySounds>)) as ISoundManager<GamePlaySounds>;
          //soundManager.PlaySound((GamePlaySounds)random.Next((int)GamePlaySounds.PackPick1, (int)GamePlaySounds.PackPick3 + 1));
        }

      }
    }

    //void BoardManager_OnTurnOver()
    //{
    //  if (CurrentPlayer.IsHuman)
    //  {
    //    if (CurrentPlayer.Cards.Any(c => c.Selected))
    //    {
    //      var card = CurrentPlayer.Cards.First(c => c.Selected);
    //      if (blocks.Any() && blocks.First().Cards.Any())
    //      {
    //        if (card.Color != blocks.First().Cards.First().Color)
    //          return;
    //      }
    //      CurrentPlayer.Cards.Remove(card);
    //      blocks.Add(new CardBlock() { Cards = new List<Card>() { card }, PlayedBy = CurrentPlayer });
    //      TurnOver();
    //    }
    //  }
    //}
  }
}
